package l1j.opqlo.NewSystem.ItemLevelUp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.utils.PerformanceTimer;
import l1j.server.server.utils.SQLUtil;

public class ItemLevelUpTable {
	private static Logger _log = Logger.getLogger(ItemLevelUpTable.class
			.getName());

	private static ItemLevelUpTable _instance;

	private final static String TOKEN = ":";

	public static ItemLevelUpTable getInstance() {
		if (_instance == null) {
			_instance = new ItemLevelUpTable();
		}
		return _instance;
	}

	private final HashMap<Integer, ItemLevelUp> _itemIdIndex = new HashMap<Integer, ItemLevelUp>();

	private ItemLevelUpTable() {
		this.loadItemLevelUP();
	}

	private void fillItemLevelUP(final ResultSet rs) throws SQLException {
		while (rs.next()) {

			final int ItemID = rs.getInt("ItemID");
			if (ItemID <= 0) {
				continue;
			}

			final boolean cansee = rs.getByte("等級顯示") == 1;

			final String str = rs.getString("力量"), dex = rs.getString("敏捷"), iint = rs
					.getString("智慧"), wis = rs.getString("精神"), con = rs
					.getString("體質"), dmg = rs.getString("大小傷害"), hit = rs
					.getString("攻擊成功"), dmg2 = rs.getString("額外攻擊"), sp = rs
					.getString("魔攻"), mr = rs.getString("魔防"), extradmg = rs
					.getString("破甲值"), extradef = rs.getString("抵抗值"), fixdmg = rs
					.getString("武器魔法基礎傷害"), randomdmg = rs
					.getString("武器魔法隨機傷害"), magicdmgmodifier = rs
					.getString("技能傷害增加"), damagereduction = rs.getString("減傷"), hp = rs
					.getString("血量"), hpr = rs.getString("回血量"), mp = rs
					.getString("魔量"), mpr = rs.getString("回魔量"), 掉寶率 = rs
					.getString("掉寶率"), 金幣率 = rs.getString("金幣率");

			final ItemLevelUp itemlevelup = new ItemLevelUp(ItemID,
					rs.getShort("MaxLevel"), str, dex, iint, wis, con, dmg,
					hit, dmg2, sp, mr, extradmg, extradef, fixdmg, randomdmg,
					magicdmgmodifier, damagereduction, hp, hpr, mp, mpr,
					cansee, 掉寶率, 金幣率);
			this._itemIdIndex.put(ItemID, itemlevelup);

		}
	}

	public void getLevelStatus(final L1ItemInstance item) {

		final ItemLevelUp check = this.getTemplate(item.getItemId());

		if (check == null) {
			return;
		}

		final int level = Math.min(item.getLevel() - 1, check._maxlevel);
		;

		if (level <= 0) {
			return;
		}
		final LevelStatus status = item.getLvStatus();

		status.Reset();

		status.AddLvStr((short) this.minus(check._str, level));

		status.AddLvDex((short) this.minus(check._dex, level));

		status.AddLvInt((short) this.minus(check._iint, level));

		status.AddLvWis((short) this.minus(check._wis, level));

		status.AddLvCon((short) this.minus(check._con, level));

		status.AddLvDmg(this.minus(check._dmg, level));

		status.AddLvHitModifier(this.minus(check._hit, level));

		status.AddLvDmgModifier(this.minus(check._dmg2, level));

		status.AddLvSP(this.minus(check._sp, level));

		status.AddLvMR(this.minus(check._mr, level));

		status.AddLvExtraDmg(this.minus(check._extradmg, level));

		status.AddLvExtraDef(this.minus(check._extradef, level));

		status.AddLvFixDmg(this.minus(check._fixdmg, level));

		status.AddLvRandomDmg(this.minus(check._randomdmg, level));

		status.AddLvMagicDmgModifier(this.minus(check._magicdmgmodifier, level));

		status.AddLvDmgReduction(this.minus(check._damagereduction, level));

		status.AddLvHp(this.minus(check._hp, level));

		status.AddLvHpr(this.minus(check._hpr, level));

		status.AddLvMp(this.minus(check._mp, level));

		status.AddLvMpr(this.minus(check._mpr, level));

		status.isCanSee(check._cansee);

		status.Add掉寶率(this.minus(check._掉寶率, level));

		status.Add金幣率(this.minus(check._金幣率, level));

	}

	public ItemLevelUp getTemplate(final int itemId) {
		return this._itemIdIndex.get(itemId);
	}

	private void loadItemLevelUP() {
		final PerformanceTimer timer = new PerformanceTimer();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM Z_opqlo_ItemLevelUP");
			rs = pstm.executeQuery();
			this.fillItemLevelUP(rs);
		} catch (final SQLException e) {
			_log.log(Level.SEVERE,
					"error while creating Z_opqlo_ItemLevelUP table", e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		_log.info("載入物品升級資料數量: " + _itemIdIndex.size() + "(" + timer.get() + "ms)");
	}

	private int minus(final String s, final int level) {
		final StringTokenizer st = new StringTokenizer(s, TOKEN);
		final int iSize = st.countTokens();
		String sTemp = null;

		final int[] iReturn = new int[iSize];
		for (int i = 0; i < iSize; i++) {
			sTemp = st.nextToken();
			iReturn[i] = Integer.parseInt(sTemp);
		}
		if (iReturn[0] > 0) {
			return iReturn[1] * (level / iReturn[0]);
		} else {
			return 0;
		}
		// item.setAddStr((short) (check._addstr *(Level / check._str_rate))) ;
		// return iReturn;
	}
}
